//
//  LLGoodsConnectionViewCell.m
//  lrlzapp
//
//  Created by dl on 16/3/2.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLGoodsConnectionViewCell.h"
#import "LLDashesLineView.h"

@interface CellTagsBtn : UIButton
@property(nonatomic, copy) NSString *tagStr;
@end

@interface LLGoodsConnectionViewCell ()
@property(nonatomic, strong) LLGoodsBasicViewModel *viewModel;

@property(nonatomic, strong) UILabel *goodsType; // 会员或者天猫
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UIView *tagView;
@property(nonatomic, strong) UIView *priceView;
@property(nonatomic, strong) TTTAttributedLabel *priceLabel;
@property(nonatomic, strong) UILabel *salesLabel;

@property(nonatomic, strong) LLUIImageView *goodsImgView;
@property(nonatomic, strong) LLUIImageView *soldOutImgView;
@property(nonatomic, strong) UIView *bottomView;
@property(nonatomic, strong) UIView *titleView;

@property(nonatomic, strong) NSArray<CellTagsBtn *> *tagBtns;
@end

@implementation LLGoodsConnectionViewCell
@synthesize goodsType, titleLabel, tagView, priceView, priceLabel, salesLabel, goodsImgView, soldOutImgView, bottomView, titleView, tagBtns;

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addEdgeLines];

        // 商品图片
        goodsImgView = [[LLUIImageView alloc] initWithFrame:CGRectMake(SUIT_LINE_WIDTH,
                SUIT_LINE_WIDTH,
                self.ll_width - 1.f,
                self.ll_width - 1.f)];
        goodsImgView.showBottomGradientLayer = YES;
        [self.contentView addSubview:goodsImgView];

        soldOutImgView = [[LLUIImageView alloc] initWithFrame:goodsImgView.frame];
        [soldOutImgView setImage:[UIImage imageNamed:@"goods_sold_out"]];
        soldOutImgView.layer.opacity = 0.6f;
        [self.contentView addSubview:soldOutImgView];

        // 底部view
        bottomView = [[UIView alloc] initWithFrame:CGRectMake(SUIT_LINE_WIDTH,
                goodsImgView.ll_height + goodsImgView.ll_origin.y,
                goodsImgView.ll_width,
                self.ll_height - goodsImgView.ll_height - 1.f)];
        [self.contentView addSubview:bottomView];

        // 标题view
        titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bottomView.ll_width, SUIT_H(35))];
        [bottomView addSubview:titleView];

        // 商标（天猫/会员）
        goodsType = [[UILabel alloc] initWithFrame:CGRectMake(SUIT_W(5), 3.f, SUIT_W(28), SUIT_H(12))];
        goodsType.backgroundColor = UI_COLOR_RED;
        goodsType.layer.cornerRadius = 2.f;
        goodsType.clipsToBounds = YES;
        goodsType.font = SUIT_FONT_320(10);
        goodsType.textColor = [UIColor whiteColor];
        goodsType.textAlignment = NSTextAlignmentCenter;
        [titleView addSubview:goodsType];

        // 商品标题
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SUIT_W(5), 0, titleView.ll_width - SUIT_W(15), SUIT_H(35))];
        titleLabel.numberOfLines = 2;
        titleLabel.textAlignment = NSTextAlignmentLeft;
        [titleView addSubview:titleLabel];

        // 商品标签
        tagView = [[UIView alloc] initWithFrame:CGRectMake(0.f, SUIT_H(35), bottomView.ll_width, SUIT_H(25))];
        [bottomView addSubview:tagView];
        priceView = [[UIView alloc] initWithFrame:CGRectMake(0.f, SUIT_H(60), bottomView.ll_width, SUIT_H(25))];
        [bottomView addSubview:priceView];

        // 虚线
        LLDashesLineView *dashesLine = [[LLDashesLineView alloc] initWithFrame:CGRectMake(0.f, 0.f, priceView.ll_width, 2 * SUIT_LINE_WIDTH)];
        dashesLine.lineColor = UIColorFromRGB(0xE5E5E5);
        [priceView addSubview:dashesLine];

        // 价格
        priceLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(SUIT_W(5),
                2 * SUIT_LINE_WIDTH,
                0.f,
                SUIT_H(24.f) - 2 * SUIT_LINE_WIDTH)];
        [priceView addSubview:priceLabel];

        // 销量
        salesLabel = [[UILabel alloc] initWithFrame:CGRectMake(priceView.ll_width - SUIT_W(105),
                2 * SUIT_LINE_WIDTH,
                SUIT_W(100),
                SUIT_H(24.f) - 2 * SUIT_LINE_WIDTH)];
        salesLabel.font = SUIT_FONT_320(13);
        salesLabel.textColor = UI_COLOR_FONT_LIGHT_GREY;
        salesLabel.textAlignment = NSTextAlignmentRight;
        [priceView addSubview:salesLabel];

        CellTagsBtn *tagBtn0 = [[CellTagsBtn alloc] initWithFrame:CGRectZero];
        [tagView addSubview:tagBtn0];
        CellTagsBtn *tagBtn1 = [[CellTagsBtn alloc] initWithFrame:CGRectZero];
        [tagView addSubview:tagBtn1];
        CellTagsBtn *tagBtn2 = [[CellTagsBtn alloc] initWithFrame:CGRectZero];
        [tagView addSubview:tagBtn2];

        tagBtns = @[tagBtn0, tagBtn1, tagBtn2];
    }
    return self;
}

- (void)addEdgeLines {
    for (int i = 0; i < 4; i++) {
        UIView *line = [UIView new];
        line.backgroundColor = UIColorFromRGB(0xE5E5E5);
        [self.contentView addSubview:line];

        if (i == 0 || i == 2) {
            line.ll_size = CGSizeMake(self.ll_width, SUIT_LINE_WIDTH);
        } else {
            line.ll_size = CGSizeMake(SUIT_LINE_WIDTH, self.ll_height);
        }
        if (i == 1 || i == 2) {
            line.ll_left = 0.f;
            line.ll_bottom = self.ll_height;
        } else {
            line.ll_right = self.ll_width;
            line.ll_top = 0.f;
        }
    }
}

- (void)bindViewModel:(id)viewModel {
    self.viewModel = viewModel;

    [self.goodsImgView setImageUrlStr:self.viewModel.goodsBasic.imageUrl size:IMAGE_SIZE_HALF_SCREEN];
    self.soldOutImgView.hidden = !self.viewModel.isSoldOut;
    self.goodsType.hidden = self.viewModel.isTmallGoods;
    self.goodsType.text = self.viewModel.goodsTypeStr;
    self.titleLabel.attributedText = self.viewModel.titleAttr;
    self.priceLabel.attributedText = self.viewModel.priceAttr;
    self.priceLabel.ll_width = self.viewModel.priceLabelWidth;

    [self.viewModel.tagViewModels enumerateObjectsUsingBlock:^(LLgoodsTagViewModel *vm, NSUInteger idx, BOOL *_Nonnull stop) {
        self.tagBtns[idx].hidden = vm.hiden;
        [self.tagBtns[idx] setTitle:vm.tagStr forState:UIControlStateNormal];
        self.tagBtns[idx].frame = vm.tagFrame;
    }];
    self.salesLabel.text = self.viewModel.salesStr;
}

@end

#pragma mark -
#pragma mark CellTagsBtn

// 设置为BTN以防后续需要点击事件
@implementation CellTagsBtn

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.font = SUIT_FONT_320(10);
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.layer.borderColor = UIColorFromRGB(0xB1DEE9).CGColor;
        self.layer.borderWidth = 2 * SUIT_LINE_WIDTH;
        self.layer.cornerRadius = 2.f;
        [self setTitleColor:UIColorFromRGB(0x62BDD2) forState:UIControlStateNormal];
        [self setTitleColor:UIColorFromRGB(0x62BDD2) forState:UIControlStateDisabled];
        self.enabled = NO;
    }
    return self;
}

@end